package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.bean.SearchBeen;
import cn.xfdzx.android.apps.shop.contract.HomeRetailLevelContract;
import cn.xfdzx.android.apps.shop.contract.HomeSearchContract;
import cn.xfdzx.android.apps.shop.model.HomeRetailLevelModel;
import cn.xfdzx.android.apps.shop.model.HomeSearchModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class HomeRetailLevelPresenter extends BasePresenter<HomeRetailLevelContract.View> implements HomeRetailLevelContract.Presenter {

    private HomeRetailLevelContract.Model model;

    public HomeRetailLevelPresenter() {
        model = new HomeRetailLevelModel();
    }


    @Override
    public void initSearchData(boolean isRefresh, boolean isLoading, String lng,  String lat, String sid, String page, String limit, String index_status, String is_store, String store_id, String keyword, String priceOrder, String salesOrder, String news) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading) {
            mView.showLoading();
        }
        model.initSearchData(lng,lat,sid, page, limit, index_status, is_store, store_id, keyword, priceOrder, salesOrder, news).compose(RxScheduler.<HomeSearchXFDGoodBeen>Flo_io_main())
                .as(mView.<HomeSearchXFDGoodBeen>bindAutoDispose()).subscribe(new Consumer<HomeSearchXFDGoodBeen>() {
            @Override
            public void accept(HomeSearchXFDGoodBeen baseObjectBean) throws Exception {
                try {
                    mView.onInitSearchDataSuccess(baseObjectBean);
                    if (isLoading) {
                        mView.hideLoading();
                    }
                    if (isRefresh) {
                        mView.finishRefresh();
                    } else {
                        mView.finishLoadMore();
                    }
                }catch (Exception e){

                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                try {
                    mView.onError(throwable);
                    if (isLoading) {
                        mView.hideLoading();
                    }
                    if (isRefresh) {
                        mView.finishRefresh();
                    } else {
                        mView.finishLoadMore();
                    }
                } catch (Exception e) {

                }
            }
        });
    }
}
